package com.css.fxfzxczh.modules.eqDisasterLossAssess.houseEconomicLossAssess.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.disasterSituationDataManage.lifeline.param.LifelineParam;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseEconomicLossAssess.entity.*;
import com.css.fxfzxczh.modules.eqDisasterLossAssess.houseEconomicLossAssess.service.HouseEconomicLossAssessService;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.entity.SurTaskManage;
import com.css.fxfzxczh.modules.fieldWorkManage.surTaskManage.service.SurTaskManageService;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 房屋直接经济损失评估
 */
@RestController
@RequestMapping("/houseEconomicDamageAssess")
@Slf4j
@Api(tags = "房屋直接经济损失评估")
public class HouseEconomicLossAssessController {

    @Autowired
    HouseEconomicLossAssessService houseEconomicLossAssessService;

    /**
     * 获取数据齐全的评估子区（数据包括抽样点，抽样点房屋）
     * @param xsemId    当前地震事件id
     * @return
     */
    @GetMapping("/getEvaluationSubArea")
    public RestResponse getPage(String xsemId){
        RestResponse restResponse=null;
        try{
            Map<String,Object> evaluationSubArea = houseEconomicLossAssessService.getEvaluationSubAreaList(xsemId);
            restResponse = RestResponse.succeed(evaluationSubArea);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     *  房屋破坏比计算
     * @param xsemId    当前地震事件id
     * @return
     */
    @GetMapping("/getFailureRatio")
    public RestResponse getFailureRatio(String xsemId){
        RestResponse restResponse=null;
        try{
            houseEconomicLossAssessService.getFailureRatio(xsemId);
            restResponse = RestResponse.succeed("计算成功");
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage="计算失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 房屋破坏比计算分页查询
     * @param request
     * @param xsemId    当前地震事件id
     * @param xstmId    评估子区id
     * @return
     */
    @GetMapping("/page")
    public RestResponse page(HttpServletRequest request, String xsemId,String xstmId){
        RestResponse restResponse=null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = houseEconomicLossAssessService.page(xsemId,xstmId,curPage,pageSize);
            restResponse = RestResponse.succeed(map);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     *  获取当前地震事件下的房屋破坏比数据
     * @param xsemId    当前地震事件id
     * @return
     */
    @GetMapping("/getFailureRatioByXsemId")
    public RestResponse getFailureRatioByXsemId(String xsemId){
        RestResponse restResponse=null;
        try{
            List<HouseFailureRatioEntity> list=houseEconomicLossAssessService.getFailureRatioByXsemId(xsemId);
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     *  获取当前地震事件中没有计算的评估子区
     * @param xsemId    当前地震事件id
     * @return
     */
    @GetMapping("/getMessage")
    public RestResponse getMessage(String xsemId){
        RestResponse restResponse=null;
        try{
            String message = houseEconomicLossAssessService.getMessage(xsemId);
            restResponse = RestResponse.succeed(message);
            restResponse.setMessage("获取成功");
        }catch (Exception e){
            String errormessage="获取失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /**
     * 保存   房屋损失比
     * @param houseLossRatioEntity
     * @return
     */
    @PostMapping("/saveHouseLossRatio")
    public RestResponse save(@RequestBody HouseLossRatioEntity houseLossRatioEntity){
        RestResponse response = null;
        try{
            boolean res = houseEconomicLossAssessService.save(houseLossRatioEntity);
            response = RestResponse.succeed(res);
            response.setMessage("修改成功");
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 修改   房屋损失比
     * @param houseLossRatioEntity
     * @return
     */
    @PostMapping("/updateHouseLossRatio")
    public RestResponse updateHouseLossRatio(@RequestBody HouseLossRatioEntity houseLossRatioEntity){
        RestResponse response = null;
        try{
            boolean res = houseEconomicLossAssessService.updateHouseLossRatio(houseLossRatioEntity);
            response = RestResponse.succeed(res);
            response.setMessage("修改成功");
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 房屋损失比
     * @param xsemId    当前地震id
     * @return
     */
    @GetMapping("/getHouseLossRatio")
    public RestResponse save(String xsemId){
        RestResponse response = null;
        try{
            HouseLossRatioEntity houseLossRatioEntity = houseEconomicLossAssessService.getHouseLossRatio(xsemId);
            response = RestResponse.succeed(houseLossRatioEntity);
            response.setMessage("查询成功");
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 房屋直接经济损失计算分页查询
     * @param request
     * @param xsemId    当前地震事件id
     * @param xstmId    评估子区id
     * @return
     */
    @GetMapping("/getDirectPage")
    public RestResponse directPage(HttpServletRequest request, String xsemId,String xstmId){
        RestResponse restResponse=null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> map = houseEconomicLossAssessService.directPage(xsemId,xstmId,curPage,pageSize);
            restResponse = RestResponse.succeed(map);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 评估子区各类房屋不同破坏状态下的房屋直接经济损失
     * @param map      xsemId     当前地震事件id
     * @return
     */
    @PostMapping("/getDirectEconomicLosses")
    public RestResponse getDirectEconomicLosses(@RequestBody Map<String,String> map){
        RestResponse restResponse=null;
        try{
            houseEconomicLossAssessService.getDirectEconomicLosses(map.get("xsemId"));
            restResponse = RestResponse.succeed();
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage = e.getMessage();
            if(StringUtils.isBlank(errormessage)) {
                errormessage = "计算失败";
            }
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 评估子区和灾区房屋直接经济损失
     * @param map       xsemId:当前地震事件id
     * @return
     */
    @PostMapping("/getDisasterAreasHousesDirectEconomicLosses")
    public RestResponse getDisasterAreasHousesDirectEconomicLosses(@RequestBody Map<String,String> map){
        RestResponse restResponse=null;
        try{
            List<DisasterAreasHousesDirectEconomicLosses> list = houseEconomicLossAssessService.getDisasterAreasHousesDirectEconomicLosses(map.get("xsemId"));
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage="计算失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 按行政区划汇总不同用途下的房屋直接经济损失
     * @param map       xsemId:当前地震事件id
     * @return
     */
    @PostMapping("/getAdministrationAreaHousesDirectEconomicLosses")
    public RestResponse getAdministrationAreaHousesDirectEconomicLosses(@RequestBody Map<String,String> map){
        RestResponse restResponse=null;
        try{
            houseEconomicLossAssessService.getAdministrationAreaHousesDirectEconomicLosses(map.get("xsemId"));
            restResponse = RestResponse.succeed("计算成功");
            restResponse.setMessage("计算成功");
        }catch (Exception e){
            String errormessage="计算失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查询行政区划汇总不同用途下的房屋直接经济损失
     * @param xsemId       当前地震事件id
     * @return
     */
    @GetMapping("/getAdministrationData")
    public RestResponse getAdministrationData(String xsemId){
        RestResponse restResponse=null;
        try{
            List<AdministrationAreaHousesDirectEconomicLosses> list = houseEconomicLossAssessService.getAdministrationData(xsemId);
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 查询当前地震事件下    评估子区和灾区房屋直接经济损失
     * @param xsemId
     * @return
     */
    @GetMapping("/getDisasterAreasHousesData")
    public RestResponse getDisasterAreasHousesData(String xsemId){
        RestResponse restResponse=null;
        try{
            List<DisasterAreasHousesDirectEconomicLosses> list = houseEconomicLossAssessService.getDisasterAreasHousesData(xsemId);
            restResponse = RestResponse.succeed(list);
            restResponse.setMessage("查询成功");
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
